package com.auoem.radio.common.custview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.auoem.radio.R;
import com.auoem.radio.common.utils.ResourceUtils;
import com.auoem.radio.databinding.FragmentMsgDialogBinding;
import com.auctus.am.framework.roomdb.entity.AmMsg;

public class MsgDialogFragment extends DialogFragment {

    private FragmentMsgDialogBinding mBinding;

    private AmMsg mAmMsg;

    public MsgDialogFragment(@NonNull AmMsg amMsg) {
        mAmMsg = amMsg;
    }

    public void setMsg(AmMsg amMsg) {
        mAmMsg = amMsg;
        update();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = FragmentMsgDialogBinding.inflate(inflater, container, false);
        initView();
        return mBinding.getRoot();
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    private void initView() {
        mBinding.cancelButton.setOnClickListener(v -> {
            dismiss();
        });
        update();
    }

    private void update() {
        mBinding.tvSrcName.setText(ResourceUtils.getResourcesString(R.string.hl_msg_dialog_src, mAmMsg.getSrcName()));
        mBinding.tvTime.setText(ResourceUtils.getResourcesString(R.string.hl_msg_dialog_time, mAmMsg.getDipTime()));

        if (mAmMsg.isGrp()) {
            mBinding.tvTarName.setText(ResourceUtils.getResourcesString(R.string.hl_msg_dialog_tar_grp, mAmMsg.getTarName()));
        } else {
            mBinding.tvTarName.setText(ResourceUtils.getResourcesString(R.string.hl_msg_dialog_tar_ind, mAmMsg.getTarName()));
        }

        mBinding.tvContent.setText(mAmMsg.getMsg());
    }
}
